<template>
    <div class="share" @click="share">
        <i class="icon njfont nj-fenxiang2"></i>
    </div>
</template>
<script>
    import {MessageBox} from "../../ui-components";
    export default {
        props: {
            url: {
                type: String,
                default: ""
            },
            title: {
                type: String,
                default: "宁家商城新品上市"
            }
        },
        data() {
            return {

            }
        },
        computed: {
            content() {
                return `钜惠${new Date().getMonth() + 1}月，宁家商城全新升级，一路嗨翻天`
            }
        },
        methods: {
            share() {
                if (!localStorage.getItem("sessionId")) {
                    MessageBox.alert('请先登录').then(action => {
                        this.$router.push({path: "login", query: {page: "1"}});
                    });
                    return;
                }
                this.SharePlugin(
                    function (message) {
                        if (message == '分享成功') {    
                            //分享成功后送礼                    
                            console.log("分享成功");
                        } else {
                            alert(message);
                            return false;
                        }
                    },
                    function (err) { console.log("Error: " + err); }
                );
            },
            SharePlugin(sucFunc, failFunc, url, opts, className) {
                sucFunc = sucFunc || function () { };
                failFunc = failFunc || function () { };
                url = url || 'Share';
                opts = opts || {
                    "title": this.title, 
                    "content": this.content, 
                    "url": this.url,  
                    "imageurl":""
                };
                console.log(opts);
                console.log(cordova);
                className = className || null;
                cordova.exec(sucFunc, failFunc, "SharePlugin", "start", [className, url, opts]);
            }

        }
    }
</script>
<style scoped>
    .share {
        position: absolute;
        right: 0.75rem;
        top: 0.2rem
    }
    .share i{
        font-size: 1.4rem;
        color:#a0a0a0
    }
</style>